<?php
class KeyController extends App_Zend_Controller_Action_Shop
{
	public function indexAction()
	{
		$start = $this->getRequest()->getParam('key', '');
		$page = $this->getRequest()->getParam('page', 1);		
		$pageNum = 100;
		
		if ($start == 'all') {
			$start = '';
		}
		
		$keyModel = new CKeywords();
		$keyList = $keyModel->getAll($page, $pageNum, $start);
		$pageCount = $keyModel->getPageCount($pageNum, $start);
		
		$first = array();
		$i=0;
		foreach($keyList as $v) {
			if ($i<5) {
				$first[]= $v['keywords'];
			}
			$i++;
		}
		$first = implode(',', $first);
		$this->view->webSiteTitle = 'Products Directory on newjerseysoccer.net for ' . $first;
		$this->view->webSiteKeywords = $first;
		$this->view->webSiteDescription = 'Jersey shirt supply for ' . $first;
		
		if ($start) {
			$prefix = $start;
		} else {
			$prefix = 'all';
		}
		
		$page = $keyModel->divPages($pageCount, $page, '/letter/' . $prefix);
		
		$this->view->page = $page;
		$this->view->keyList = $keyList;
		$this->view->keyModel = $keyModel;
	}
	
	public function detailAction()
	{
		$this->view->styleName = 'product-list';
		$this->view->otherStyleName = 'product';
		$keyword=$this->_request->getParam('key');
		if($keyword == ''){
			$this->_forward('index','error');
			return false;
		}
		
		$page = $this->_request->getParam('page',1);
		
		
		$oldKeyword = $keyword;
		$keyword = str_replace('-', ' ', $keyword);
		$item = new CItem();
		$this->getHelper('Fenlei')->getCategories();

		//L　SEARCH
		
		$search = new CSearch();
		$itemCounts = $search->search2($keyword,$page,24,1);	
		if($itemCounts == 0){
			$result = $search->search3($keyword,$page,24);	
			$itemCounts = $search->search3($keyword,$page,24,1);
		}else{
			$result = $search->search2($keyword,$page,24);	
		}
		
		if($result==0){
			$result = array();
		}
		
		Zend_Loader::loadClass("CFilter");
    	$pageArray = CFilter::filterPage ($page, $itemCounts, 24);

		$itemArray = array();
		$myCache = Zend_Registry::get('myCache');
		$itemMap = CCache::getCombinlistItemMap();
		$crawl = new CCrawl();			
		
		$productResult = $search->searchProduct2($keyword);
		if($productResult==0){
			$productResult = array();
		}
		for($index=0,$len=count($productResult);$index<$len;$index++){
			array_unshift($itemArray,$itemMap[$productResult[$index]]);
			if($index==1){
				break;
			}
		}
		
		foreach ($result as $v) {
			if ($v['attrs']['is_show'] == 1) {
				$has = false;
				foreach($itemArray as $it){
					if($v['id']==$it['itemid']){
						$has = true;
						break;
					}
				}
				if(!$has){
					array_push($itemArray,$itemMap[$v['id']]);	
				}
			} else {
				array_push($itemArray,$crawl->getCrawlItems(array($v['id'])));				
			}
		}
		if($page==1){
			foreach($itemArray as $item){
				if(isset($item['subject'])){
					$this->view->itemDetailArray = array($item);
					define('OLDBADGE', 0);
					$select_badgepatch = null;
			        if(!empty($itemDetailArray[0]['select_badgepatch_p'])) {
			            $select_badgepatch = unserialize($itemDetailArray[0]['select_badgepatch_p']);
			        }
			        if (!OLDBADGE) {  
			        	$badge = new CBadge();
			        	$select_badgepatch = $badge->getItemBadge($item['itemid']);
			        }        
			        $this->view->assign('select_badgepatch',is_array($select_badgepatch)?$select_badgepatch:array());
					
					if(isset($item['subject'])){
						$parentArray=$this->getHelper('Fenlei')->getAllParents($this->view,$item['flid']);
						$parentArray[]=$item['subject'];
						$this->view->parentArray = $parentArray;
					}
					break;
				}
			}
			if(!$this->view->itemDetailArray){
				$myCache = Zend_Registry::get('myCache');
				$itemMap = CCache::getCombinlistItemMap();
				$imcount = count($itemMap);
				$md5Keyword = md5($keyword);
				$mcount = 0;
				for($i=0;$i<strlen($md5Keyword);$i++){
					$mcount+=ord($md5Keyword[$i]);
				}
				
				$mcount = $mcount%$imcount;
				$index = 0;
				$keys = array_keys($itemMap);
				foreach($keys as $key){
					if($index == $mcount){
						$this->view->itemDetailArray = array($itemMap[$key]);
						break;
					}
					$index++;
				}
			}
		}
		
		if(count($itemArray)==0){
			$this->getHelper('layout')->disableLayout();
			$this->getHelper('viewRenderer')->setNoRender();
			$contentType = 'text/html';
			header("Content-Type: $contentType; charset=utf-8");
			header("HTTP/1.1 404 Not Found");
			readfile('404.shtml');
			return;
		}
		
		//竞争网站
		$myCache = Zend_Registry::get('myCache');
		$jingzhengwebsites = CCache::getJingzhengwebsites();
		$imcount = count($jingzhengwebsites);
		$md5Keyword = md5($keyword);
		$mcount = 0;
		for($i=0;$i<strlen($md5Keyword);$i++){
			$mcount+=ord($md5Keyword[$i]);
		}
		$jingzhengweb = '';
		$mcount = $mcount%$imcount;
		$index = 0;
		foreach($jingzhengwebsites as $value){
			if($index == $mcount){
				$jingzhengweb = $value;
				break;
			}
			$index++;
		}
		
		$keyModel = new CHotSearch();
		
    	//$pageString =$keyModel->divPages( $pageArray ['pageNumber'], $pageArray ['page'], '/so/' . $oldKeyword); 		
		//$this->view->assign ( "pageString", $pageString );
    	$this->view->assign ( "pageArray", $pageArray );
		$this->view->assign('itemArray',$itemArray);
		$this->view->assign('itemCounts',$itemCounts);
		//$this->view->assign('order',$order);
		$this->view->assign('page',$page);
		$this->view->assign('keyword',$keyword);
		$myRandom = new CMyRandom();
		$discountOff = $myRandom->getRandomNumberMinMax($keyword, 10,60);
		$blackFridayKeyArray = CCache::getBlackFridayKeyArray();
		$blackIndex = $myRandom->getRandomNumber($keyword, count($blackFridayKeyArray));
		
		$this->view->discountOff = $discountOff;
		$this->view->blackFridayKey = $blackFridayKeyArray[$blackIndex];
		
		$this->view->webSiteTitle = $keyword .', cheap '.$keyword.' on '.$jingzhengweb;
		$this->view->webSiteKeywords = $keyword.','.$blackFridayKeyArray[$blackIndex].','.$jingzhengweb;
		$this->view->webSiteDescription = $keyword . ', '.$discountOff.'% off '.$blackFridayKeyArray[$blackIndex].', free Shipping soccer jersey shirt Supply, wholesale jersey shirts';

		$csearch = new CSearch();
		$relatedKeywordList = $csearch->searchKey($keyword, 'any', 11);
		array_shift($relatedKeywordList);
		$this->view->randomKeywords = $relatedKeywordList;
		
		$randomHotKeywords = $keyModel->getRandomKeywords(5,$keyword);
		$this->view->randomHotKeywords = $randomHotKeywords;
		
		//SET META FOR FACEBOOK
        $this->getHelper ('layout')->getView()->headMeta()->setName('og:title', $keyword);
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:type', 'jersery');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:url', MY_URL);
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:image', MY_URL.'/images/logo.jpg');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:site_name', 'newjerseysoccer.net');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:description', $keyword);
		
		/*属性筛选*/
		$attr = new CAttr();
        $categoryAttr = $attr->getByType(1);	
        $brandAttr = $attr->getByType(2);	
        $playerAttr = $attr->getByType(3);
        
        $this->view->categoryAttr=$categoryAttr;
        $this->view->brandAttr=$brandAttr;
        $this->view->playerAttr = $playerAttr;
        
        $this->view->isshowchoose = 1;
	}
	
	public function superdetailAction()
	{
		$this->view->styleName = 'product-list';
		$this->view->otherStyleName = 'product';
		$keyword=$this->_request->getParam('key');
		if($keyword == ''){
			$this->_forward('index','error');
			return false;
		}
		
		$page = $this->_request->getParam('page',1);	
		
		$oldKeyword = $keyword;
		$keyword = str_replace('-', ' ', $keyword);
		$item = new CItem();
		$this->getHelper('Fenlei')->getCategories();
	
		$search = new CSearch();
		$itemCounts = $search->search2($keyword,$page,24,1);	
		if($itemCounts == 0){
			$result = $search->search3($keyword,$page,24);	
			$itemCounts = $search->search3($keyword,$page,24,1);
		}else{
			$result = $search->search2($keyword,$page,24);	
		}
		
		if($result==0){
			$result = array();
		}
		
		$itemArray = array();
		$crawl = new CCrawl();	
		
		$myCache = Zend_Registry::get('myCache');
		$itemMap = CCache::getCombinlistItemMap();
		
		$productResult = $search->searchProduct2($keyword);
		if($productResult==0){
			$productResult = array();
		}
		for($index=0,$len=count($productResult);$index<$len;$index++){
			array_unshift($itemArray,$itemMap[$productResult[$index]]);
			if($index==1){
				break;
			}
		}
		
		Zend_Loader::loadClass("CFilter");
    	$pageArray = CFilter::filterPage ($page, $itemCounts, 24);
		
		foreach ($result as $v) {
			if ($v['attrs']['is_show'] == 1) {
				$has = false;
				foreach($itemArray as $it){
					if($v['id']==$it['itemid']){
						$has = true;
						break;
					}
				}
				if(!$has){
					array_push($itemArray,$itemMap[$v['id']]);	
				}
			} else {
				array_push($itemArray,$crawl->getCrawlItems(array($v['id'])));				
			}
		}
		
		if(count($itemArray)==0){
			$this->getHelper('layout')->disableLayout();
			$this->getHelper('viewRenderer')->setNoRender();
			$contentType = 'text/html';
			header("Content-Type: $contentType; charset=utf-8");
			header("HTTP/1.1 404 Not Found");
			readfile('404.shtml');
			return;
		}
		
		$keyModel = new CKeywords();
		
    	//$pageString =$keyModel->divPages( $pageArray ['pageNumber'], $pageArray ['page'], '/product/' . $oldKeyword); 		
    	$this->view->assign ( "pageArray", $pageArray );
		$this->view->assign('itemArray',$itemArray);
		$this->view->assign('itemCounts',$itemCounts);
		$this->view->assign('page',$page);
		$this->view->assign('keyword',$keyword);
		$this->view->webSiteTitle = $keyword;
		$this->view->webSiteKeywords = $keyword;
		$this->view->webSiteDescription = 'Buy '.$keyword . ',cheap '.$keyword.' soccer jerseys for sale,wholesale '.$keyword.' jersey shirts';
		
		$randomKeywords = $keyModel->getRandomKeywords(10,$keyword);
		$this->view->randomKeywords = $randomKeywords;
		
		//SET META FOR FACEBOOK
        $this->getHelper ('layout')->getView()->headMeta()->setName('og:title', $keyword);
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:type', 'jersery');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:url', MY_URL);
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:image', MY_URL.'/images/logo.jpg');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:site_name', 'newjerseysoccer.net');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:description', $keyword);
		
		/*属性筛选*/
		$attr = new CAttr();
        $categoryAttr = $attr->getByType(1);	
        $brandAttr = $attr->getByType(2);	
        $playerAttr = $attr->getByType(3);
        
        $this->view->categoryAttr=$categoryAttr;
        $this->view->brandAttr=$brandAttr;
        $this->view->playerAttr = $playerAttr;
        
        $this->view->isshowchoose = 1;
	}
	/**
	public function testAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		ignore_user_abort();
		set_time_limit(0);
		
		
		$sql = 'select * from zcshop_keywords';
		
		$member = new CMember();
		$db = $member->getAdapter();
		
		$result = $db->fetchAll($sql);	
		
		foreach ($result as $v) {			
			$sql = 'update zcshop_keywords set start="' . strtoupper(substr($v['keywords'], 0, 1)) . '" where keywords_id=' . $v['keywords_id'];
			$db->query($sql);	
		}		
	}
	
	public function relateAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		ignore_user_abort();
		set_time_limit(0);
		
		mysql_connect('localhost', 'admin', 'qa1234');
		mysql_select_db('newjerseysoccer');
		$result = mysql_query('select * from zcshop_keywords');
		$search = new CSearch();
		
		$member = new CMember();
		$db = $member->getAdapter();		
		
		while ($row = mysql_fetch_array($result)) {	
			$return = $search->searchProduct($row['keywords']);	
			foreach ($return as $v) {
				$sql1 = 'select productid from zcshop_products where itemid=' . $v;
				$result1 = mysql_query($sql1);
				$row1 = mysql_fetch_array($result1);
				$sql = 'insert into `zcshop_product_keywords_related` (productid, keywords_id) values (' . $row1['productid'] . ', ' . $row['keywords_id'] . ')';
				try {
					$db->query('insert into `zcshop_product_keywords_related` (productid, keywords_id) values (' . $row1['productid'] . ', ' . $row['keywords_id'] . ')');
				} catch (Exception $e) {
					echo $e->getMessage();
				}
				
			}
		}
	}
	
	public function unshowAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		ignore_user_abort();
		set_time_limit(0);
		
		
		$sql = 'select i.*,f.* from zcshop_items i inner join zcshop_productfls f on i.flid=f.flid where i.item_type="product"';
		$member = new CMember();
		$db = $member->getAdapter();
		
		$result = $db->fetchAll($sql);
		
		foreach ($result as $v) {			
			if ($v['pid'] != 92 && $v['pid'] != 93) {
				try {
					echo 'update zcshop_products set is_showed=0 where itemid=' . $v['itemid'] . '<br/>';
					$db->query('update zcshop_products set is_showed=0 where itemid=' . $v['itemid']);
				} catch (Exception $e) {
					echo $e->getMessage();
				}
			} else {				
				if ($v['openflag'] == 0) {					
					echo 'update zcshop_products set is_showed=0 where itemid=' . $v['itemid'] . '<br/>';
					$db->query('update zcshop_products set is_showed=0 where itemid=' . $v['itemid']);
				}
			}
		}
	}
	
	public function updatekeynumAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		ignore_user_abort();
		set_time_limit(0);
		
//		$keyModel = new CKeywords();
//		$keyList = $keyModel->fetchAll();
//		$db = $keyModel->getAdapter();
		mysql_connect('localhost', 'admin', 'qa1234');
		mysql_select_db('newjerseysoccer');
		$result = mysql_query('select * from zcshop_keywords');
		$search = new CSearch();
		
		while ($row = mysql_fetch_array($result)) {	
			$itemCounts = $search->search2($row['keywords'],1,12,1);	
			$sql = 'update zcshop_keywords set product_num=' . $itemCounts . ' where keywords_id=' . $row['keywords_id'];
			mysql_query($sql);
		}
	}
	
	public function testsphinxAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		ignore_user_abort();
		set_time_limit(0);
		$search = new CSearch();
		$result = $search->search($this->getRequest()->getParam('key', 'soccer'), 10);
		print_r($result);
	}
	
	public function addkeyAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$key = $this->getRequest()->getParam('key', '');
		if (!empty($key)) {
			$keyModel = new CKeywords();
			$row = $keyModel->fetchRow(array('keywords = ?'=>$key));
			
			if (!$row) {
				$keyModel->insert(array('keywords'=>$key,'is_show'=>1, 'start'=>strtoupper(substr($key, 0, 1))));
			}
		}		
	}	
	*/
	public function updatecrawlimgAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
	
		Zend_Loader::loadClass('CFile');
		Zend_Loader::loadClass('CString');
		Zend_Loader::loadClass('CPath');
		
		ignore_user_abort();
		set_time_limit(0);
		
		$crawl = new CCrawl2();
		$data = $crawl->getAll("SELECT * from zcshop_crawl_item2 where cutUrl='' order by id asc limit 100");
		while(count($data) > 0){ 
			$yearmonth = date ( "Ym" );
			for($i=0;$i<count($data);$i++){
				try{
					$str = strtolower(CString::randYzStr(1));
					$dir300 = MY_ROOT.'/html/upload/crawl_img/'.$yearmonth.'/300x300/'.$str;
					$dir150 = MY_ROOT.'/html/upload/crawl_img/'.$yearmonth.'/150x150/'.$str;
					
					CFile::createFolder($dir300);
					CFile::createFolder($dir150);
					
					$itemId = $data[$i]['id'];
					$img = str_replace('150x150','300x300',$data[$i]['imgUrl']);
					$fileTypeArray = explode ( ".", $img );
					$type = array_pop($fileTypeArray);
					$typeArr = preg_split('/[\?\&]/',$type);
					$type = $typeArr[0];
					$fileName = $itemId.'-'.time().'.'.$type;
					$path = $dir300."/".$fileName;
					$isSuc =  CFile::openAndWrite($img,$path);
					if(!$isSuc){
						$crawl->update("update zcshop_crawl_item2 set cutUrl='no' where id='$itemId'");
						echo $img.'<br />';
					}else{
						$cclsImage = new CClsImage();
						$img150 = $cclsImage->make_thumb($path,150,150,$dir150.'/');
						$img150Path = 'html/upload/crawl_img/'.$yearmonth.'/150x150/'.$str.'/'.$img150;
						$img300Path = 'html/upload/crawl_img/'.$yearmonth.'/300x300/'.$str.'/'.$fileName;
						//echo $img150Path.'<br />';
						//echo $img300Path.'<br />';
						//echo '<br />';
						$crawl->update("update zcshop_crawl_item2 set imgUrl='$img300Path',cutUrl='$img150Path' where id='$itemId'");
					}
				}catch(Exception $e){
					$crawl->update("update zcshop_crawl_item2 set cutUrl='no' where id='$itemId'");
				}
			}
			$data = $crawl->getAll("SELECT * from zcshop_crawl_item2 where cutUrl='' order by id asc limit 100");
		}
	}
	
	public function fanyiAction(){		
		ignore_user_abort();
		set_time_limit(0);
		$proxy = array(
						array('127.0.0.1', '80'),
						array('37.128.146.134', '80'),
						array('37.128.146.15', '80'),
						array('37.128.146.3', '80')
					   );
		$index = 0;
		
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$crawl2 = new CCrawl2();
		$data = $crawl2->getAll("SELECT * from zcshop_crawl_item2 where hasAdd=0 and name<>'' and description<>'' and description<> '&nbsp' and cutUrl<>'' and cutUrl<>'no' and lang<>'en' order by id asc limit 10");
		if (empty($data[0])) {
			echo 'nothing';exit();
		}
		for($i=0;$i<count($data);$i++){
			if(trim($data[$i]['name']) == '' || trim($data[$i]['cutUrl']) == '' || trim($data[$i]['cutUrl']) == 'no'){
				continue;
			}
			if(trim($data[$i]['price']) == ''){
				$data[$i]['price'] = '$50';
			}
			if($data[$i]['lang']!='en'){
				$index = rand(0,3);					
				$google = new GoogleTranslate($data[$i]['name'], $data[$i]['lang'], 'en', $proxy[$index][0], $proxy[$index][1]);
				if(!strpos($google->result,'http://translate.google.com/translate_a')){ 
					$data[$i]['name'] = $google->result;
				}else{
					return;
				}
				$index++;
				if ($index == count($proxy)) {
					$index = 0;
				}
				sleep(1);
				$index = rand(0,3);			
				$google = new GoogleTranslate($data[$i]['description'], $data[$i]['lang'], 'en', $proxy[$index][0], $proxy[$index][1]);
				$index++;
				if ($index == count($proxy)) {
					$index = 0;
				}
				if(!strpos($google->result,'http://translate.google.com/translate_a')){
					$data[$i]['description'] = $google->result;
				}else{
					return;
				}
				if ($data[$i]['description'] == '') {
					continue;
				}
			}else{
				return;
			}
			$itemId = $data[$i]['id'];
			unset($data[$i]['id']);
			unset($data[$i]['lang']);
			unset($data[$i]['hasAdd']);
			$crawl = new CCrawl();
			try{
				$crawl->insert($data[$i]);
			}catch(Exception $e){
				
			}
			
			$crawl2->update("update zcshop_crawl_item2 set hasAdd='1' where id='$itemId'");
			
		}	
		echo 'ok';		
	}
	
	//重写当前库
	public function rewritecuraddtocrawlitemAction(){
		
		ignore_user_abort();
		set_time_limit(0);
		
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$crawl2 = new CCrawl2();
		$data = $crawl2->getAll("SELECT * from zcshop_crawl_item2 where hasAdd=0 and (site='www.shopping.uk' or site='www.shopping.au') order by id asc limit 100");
		$crawl = new CCrawl();
		while(count($data) > 0){ 
			for($i=0;$i<count($data);$i++){
				//重写
				$data[$i]['name'] = CRewrite::write($data[$i]['name']);
				$data[$i]['description'] = CRewrite::write($data[$i]['description']);
				$data[$i]['gmtModify'] = time();
				$itemId = $data[$i]['id'];
				unset($data[$i]['id']);
				unset($data[$i]['lang']);
				unset($data[$i]['hasAdd']);
				$crawl->insert($data[$i]);
				$crawl2->update("update zcshop_crawl_item2 set hasAdd='1' where id='$itemId'");
			}
			$data = $crawl->getAll("SELECT * from zcshop_crawl_item2 where hasAdd=0 and (site='www.shopping.uk' or site='www.shopping.au') order by id asc limit 100");
		}
	}
	
	public function updatecrawlimg2Action()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
	
		Zend_Loader::loadClass('CFile');
		Zend_Loader::loadClass('CString');
		Zend_Loader::loadClass('CPath');
		
		ignore_user_abort();
		set_time_limit(0);
		
		$crawl = new CCrawl();
		$data = $crawl->getAll("SELECT * from zcshop_crawl_item where cutUrl='' order by id desc limit 100");
		while(count($data) > 0){ 
			$yearmonth = date ( "Ym" );
			for($i=0;$i<count($data);$i++){
				try{
					$str = strtolower(CString::randYzStr(1));
					$dir300 = MY_ROOT.'/html/upload/crawl_img/'.$yearmonth.'/300x300/'.$str;
					$dir150 = MY_ROOT.'/html/upload/crawl_img/'.$yearmonth.'/150x150/'.$str;
					
					CFile::createFolder($dir300);
					CFile::createFolder($dir150);
					
					$itemId = $data[$i]['id'];
					$img = str_replace('150x150','300x300',$data[$i]['imgUrl']);
					$fileTypeArray = explode ( ".", $img );
					$type = array_pop($fileTypeArray);
					$fileName = $itemId.'-'.time().'.'.$type;
					$path = $dir300."/".$fileName;
					$isSuc =  CFile::openAndWrite($img,$path);
					if(!$isSuc){
						$crawl->update("update zcshop_crawl_item set cutUrl='no' where id='$itemId'");
						echo $img.'<br />';
					}else{
						$cclsImage = new CClsImage();
						$img150 = $cclsImage->make_thumb($path,150,150,$dir150.'/');
						$img150Path = 'html/upload/crawl_img/'.$yearmonth.'/150x150/'.$str.'/'.$img150;
						$img300Path = 'html/upload/crawl_img/'.$yearmonth.'/300x300/'.$str.'/'.$fileName;
						//echo $img150Path.'<br />';
						//echo $img300Path.'<br />';
						//echo '<br />';
						$crawl->update("update zcshop_crawl_item set imgUrl='$img300Path',cutUrl='$img150Path' where id='$itemId'");
					}
				}catch(Exception $e){
					$crawl->update("update zcshop_crawl_item set cutUrl='no' where id='$itemId'");
				}
			}
			$data = $crawl->getAll("SELECT * from zcshop_crawl_item where cutUrl='' order by id desc limit 100");
		}
	}
	
	private $urlArray = array();
	
	public function makesitemapindexAction(){
		
		ignore_user_abort();
		set_time_limit(0);
		
		$this->makekeysitemapAction();
		$this->makeproductsitemapAction();
		$this->makecproductsitemapAction();
		$this->makecatesitemapAction();
		$this->makehotkeysitemapAction();
		//$this->makecmnsiteAction();
		$this->makeplayersitemapAction();
		$this->makemainsitemapAction();
		$xml = '';
		$xml .= '<?xml version="1.0" encoding="UTF-8"?>';
		$xml .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
		foreach($this->urlArray as $url){
			$xml .= '<sitemap>';
			$xml .= '<loc>'.$url.'</loc>';
			$xml .= '<lastmod>'.date('Y-m-d').'</lastmod>';
			$xml .= '</sitemap>';
		}
		$xml .= '</sitemapindex>';
		file_put_contents(MY_ROOT.'/sitemap/sitemapindex.xml',$xml);
	}
	
	public function makekeysitemapAction(){
		
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$pageNum = 100;
		$keyModel = new CKeywords();
		for($i=65;$i<91;$i++){
			$pageCount = $keyModel->getPageCount($pageNum, chr($i));
			for($index=0;$index<ceil($pageCount/100);$index++){

				//file_put_contents(MY_ROOT.'/sitemap/products-'.chr($i).'-'.$index.'.xml',$xml);
				array_push($this->urlArray,MY_URL.'/sitemap/products-'.chr($i).'-'.$index.'.xml');
			}
		}
	}
	
	public function makeproductsitemapAction(){
		
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$where="is_showed=1 and zcshop_products.onsales=0 and min_price < max_price";
		$itemShow=new CItemShow();
		$itemShow->setType('product');
		$itemShow->setWhere($where);
		$itemArray=$itemShow->getAllItems();
		
		$xml = '';
		$xml .= '<?xml version="1.0" encoding="UTF-8"?>';
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
		
		for($i=0,$len=count($itemArray);$i<$len;$i++){
			$xml .= '<url>';
			$xml .= '<loc>'.MY_URL.'/pd-'.$itemArray[$i]['itemid'].'.html</loc>';
			$xml .= '<lastmod>'.date('Y-m-d').'</lastmod>';
			$xml .= '<changefreq>weekly</changefreq>';
			$xml .= '<priority>1</priority>';
			$xml .= '</url>';
		}
		
		$xml .= '</urlset>';
		file_put_contents(MY_ROOT.'/sitemap/product.xml',$xml);
		array_push($this->urlArray,MY_URL.'/sitemap/product.xml');
	}
	
	public function makecproductsitemapAction(){
		
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$count = 0;
		$index = 0;
		$keyModel = new CCrawl();
		$keyList = $keyModel->getRssCrawlsOffset(100,$index);
		$allCount = $keyModel->getPageCount(1);
		echo $allCount;echo '<br />';
		while(count($keyList)>0){
			if($count%10000 == 0){
				$xml = '';
				$xml .= '<?xml version="1.0" encoding="UTF-8"?>';
				$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			}
			$index = $index+100;
			foreach($keyList as $v) {
				if($count == $allCount){
					break;
				}
				$count++;
				$xml .= '<url>';
				$xml .= '<loc>'.MY_URL.'/football_shirt_'.$v['id'].'.html</loc>';
				$xml .= '<lastmod>'.date('Y-m-d').'</lastmod>';
				$xml .= '<changefreq>daily</changefreq>';
				$xml .= '<priority>0.9</priority>';
				$xml .= '</url>';
			}
			if($count%10000 == 0 || $count == $allCount){
				$xml .= '</urlset>';
				file_put_contents(MY_ROOT.'/sitemap/cproducts-'.$count.'.xml',$xml);
				array_push($this->urlArray,MY_URL.'/sitemap/cproducts-'.$count.'.xml');
				echo $count;echo '<br />';
			}
			echo $count;echo 'and '.$allCount.'<br />';
			if($count == $allCount){
				echo $count;echo '。ssss<br />';
				break;
			}
			$keyList = $keyModel->getRssCrawlsOffset(100,$index);
		}
	}
	
	public function makecatesitemapAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();

		$productFl = new CFenlei('product');
        $productFl->order = "en_flname ASC";
        $flArray2 = $productFl->getFenleiByPid(92);
        
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        
		foreach ($flArray2 as $v) {
			$xml.= '<url>';
    		$xml.= '<loc>';
    		$xml.= MY_URL . '/new-jersey-category-' . $v['flid'] . '-0-1-' . $this->view->encodeName($v['en_flname']) . '.html';
    		$xml.= '</loc>';
    		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
			$xml.= '<changefreq>weekly</changefreq>';
    		$xml.= '<priority>0.9</priority>';
   			$xml.= '</url>';
		}
		$xml.= '</urlset>';		
		
		file_put_contents(MY_ROOT.'/sitemap/category.xml',$xml);
		array_push($this->urlArray,MY_URL.'/sitemap/category.xml');
	}
	
	public function makehotkeysitemapAction(){
		
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$pageNum = 100;
		$keyModel = new CHotSearch();
		$pageCount = ceil($keyModel->getAllItemCounts()/$pageNum);
		for($index=0;$index<ceil($pageCount/100);$index++){
			$xml = '';
			$xml .= '<?xml version="1.0" encoding="UTF-8"?>';
			$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			for($page=1+$index*100;$page<1+$index*100+100;$page++){
				$keyList = $keyModel->getAll($page, $pageNum);
				foreach($keyList as $v) {
					$xml .= '<url>';
					$xml .= '<loc>'.MY_URL.'/soccer-jerseys/'.$keyModel->encode($v['keyword'], 0).'/</loc>';
					$xml .= '<lastmod>'.date('Y-m-d').'</lastmod>';
					$xml .= '<changefreq>weekly</changefreq>';
					$xml .= '<priority>0.8</priority>';
					$xml .= '</url>';
				}
			}
			$xml .= '</urlset>';
			file_put_contents(MY_ROOT.'/sitemap/hotproducts.xml',$xml);
			array_push($this->urlArray,MY_URL.'/sitemap/hotproducts.xml');
		}
		
		
	}
	
	public function makecmnsiteAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$itemMap = CCache::getCombinlistItemMap();
		for($k=0;$k<20;$k++){
			$xml = '';
			$xml .= '<?xml version="1.0" encoding="UTF-8"?>';
			$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			for($i=0;$i<10000;$i++){
				$items = shuffle($itemMap);
				$itemids = array();
				foreach($itemMap as $key=>$value){
					$itemids[]=$itemMap[$key]['itemid'];
					if(count($itemids)>3){
						break;
					}
				}
				$itemidStr = implode('|',$itemids);
				$xml .= '<url>';
				$xml .= '<loc>'.MY_URL.'/soccer_jersey_'.base64_encode($itemidStr).'.html</loc>';
				$xml .= '<lastmod>'.date('Y-m-d').'</lastmod>';
				$xml .= '<changefreq>weekly</changefreq>';
				$xml .= '<priority>0.7</priority>';
				$xml .= '</url>';
			}
			$xml .= '</urlset>';
			file_put_contents(MY_ROOT.'/sitemap/cmn-'.$k.'.xml',$xml);
			array_push($this->urlArray,MY_URL.'/sitemap/cmn-'.$k.'.xml');
		}
	}
	
	
	public function makeplayersitemapAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();

        $playerArray = CCache::getPlayerArray();
    	$this->view->playerArray = $playerArray;
        
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        
        $keyModel = new CHotSearch();
        
		foreach($playerArray as $playerInfo){
			$xml.= '<url>';
    		$xml.= '<loc>';
    		$xml.= MY_URL . '/player-soccer-jersey/'.$keyModel->encode($playerInfo['name']);
    		$xml.= '</loc>';
    		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
			$xml.= '<changefreq>weekly</changefreq>';
    		$xml.= '<priority>0.9</priority>';
   			$xml.= '</url>';
		}
		$xml.= '</urlset>';		
		
		file_put_contents(MY_ROOT.'/sitemap/player.xml',$xml);
		array_push($this->urlArray,MY_URL.'/sitemap/player.xml');
	}
	
	public function makemainsitemapAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();

        
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        
		$xml.= '<url>';
		$xml.= '<loc>';
		$xml.= MY_URL . '/cld-92-Soccer_Jersey_Kits.html';
		$xml.= '</loc>';
		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
		$xml.= '<changefreq>weekly</changefreq>';
		$xml.= '<priority>0.9</priority>';
		$xml.= '</url>';
		
		$xml.= '<url>';
		$xml.= '<loc>';
		$xml.= MY_URL . '/cld-93-National_Soccer_Jerseys.html';
		$xml.= '</loc>';
		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
		$xml.= '<changefreq>weekly</changefreq>';
		$xml.= '<priority>0.9</priority>';
		$xml.= '</url>';
		
		$xml.= '<url>';
		$xml.= '<loc>';
		$xml.= MY_URL . '/player-soccer-jersey';
		$xml.= '</loc>';
		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
		$xml.= '<changefreq>weekly</changefreq>';
		$xml.= '<priority>0.9</priority>';
		$xml.= '</url>';
		
		$xml.= '<url>';
		$xml.= '<loc>';
		$xml.= MY_URL . '/cno-1-new_jerseys.html';
		$xml.= '</loc>';
		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
		$xml.= '<changefreq>weekly</changefreq>';
		$xml.= '<priority>0.8</priority>';
		$xml.= '</url>';
		
		$xml.= '<url>';
		$xml.= '<loc>';
		$xml.= MY_URL . '/Black-Friday-Soccer-Jersey';
		$xml.= '</loc>';
		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
		$xml.= '<changefreq>weekly</changefreq>';
		$xml.= '<priority>0.8</priority>';
		$xml.= '</url>';
		
		$xml.= '<url>';
		$xml.= '<loc>';
		$xml.= MY_URL . '/cmn-1-package_jerseys.html';
		$xml.= '</loc>';
		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
		$xml.= '<changefreq>weekly</changefreq>';
		$xml.= '<priority>0.7</priority>';
		$xml.= '</url>';
		
		$xml.= '<url>';
		$xml.= '<loc>';
		$xml.= MY_URL . '/letter/A_1.html';
		$xml.= '</loc>';
		$xml.= '<lastmod>'.date('Y-m-d').'</lastmod>';
		$xml.= '<changefreq>weekly</changefreq>';
		$xml.= '<priority>0.7</priority>';
		$xml.= '</url>';
		
		$xml.= '</urlset>';		
		
		file_put_contents(MY_ROOT.'/sitemap/main.xml',$xml);
		array_push($this->urlArray,MY_URL.'/sitemap/main.xml');
	}
	
	
	public function fixproductdescAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		ignore_user_abort();
		set_time_limit(0);
		
		$itemMap = CCache::getCombinlistItemMap();
		$product = new CProduct();
		foreach($itemMap as $key => $value){
			$item = $itemMap[$key];
			$desc=str_replace('�',' ',$item['description']);
			$product->editProduct($item['itemid'],array('description'=>$desc));
		}
	}
	
	public function runscoreAction(){
		
		ignore_user_abort();
		set_time_limit(0);
		
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$s=$this->_request->getParam('s');
		
		$pageNum = 100;
		$keyModel = new CCrawl();
		$pageCount = $keyModel->getPageCount($pageNum);
		$scoreCount = 0;
		for($i=1;$i<100001;$i=$i+100){
			for($page=$i;$page<$i+100;$page++){
				if($page>$pageCount){
					return;
				}
				$keyList = $keyModel->getAllCrawls($page, $pageNum);
				foreach($keyList as $v) {

					$index=Zend_Search_Lucene::open(MY_ROOT."/indexdata/index");
					Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_OR);
					$query=Zend_Search_Lucene_Search_QueryParser::parse(preg_replace('/(soccer)|(jerseys?)|(football)|(\*)/i','',$v['name']),'utf-8');
					$hits=$index->find($query);
					if(count($hits)==0){
						$query=Zend_Search_Lucene_Search_QueryParser::parse(preg_replace('/(\*)/i','',$v['name']),'utf-8');
						$hits=$index->find($query);
					}
					$myHits=array();
					$maxScore = null;
					foreach($hits as $hit){
						if(!$maxScore){
							$maxScore = $hit->score;
							break;
						}
					}
					if($maxScore > $s){
						$scoreCount++;
					}
					
				}
			}
		}
		
		echo $scoreCount;
	}
	
	/*
	public function addplayerattrAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$map = CCache::getProductAttributeMap();
		$playerNameArray = $map['Player'];
		
		$playerName = $playerNameArray;
		$attrModel = new CAttr();
		for ($i = 0; $i < count($playerName); $i++) {
			if (!empty($playerName[$i])) {
				$attrModel->updateAttr($playerName[$i], $i+1, 0, 3);		
			}
		}
		echo 'ok';
	}*/
	
	/*
	public function fixcrawlimgAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		ignore_user_abort();
		set_time_limit(0);
		$crawl = new CCrawl();
		$data = $crawl->getAll();
		for($i=0;$i<count($data);$i++){
			if($data[$i]['name']=='null'&&$data[$i]['site']=='soccer.com'){

					$itemId = $data[$i]['id'];
					$url=$data[$i]['link'];
					$content = file_get_contents($url);
					preg_match("/<h1>([^<]+)<\/h1>/i", $content, $matches);
					$name = mysql_escape_string($matches[1]); 
					preg_match('/<div id="ProductAboutDiv" style="display:block">([\s\S]+?)<\/div>[\s\n\r\t]+<div id="ProductSpecsDiv" style="display:none">/i', $content, $matches2);
					$desc = $matches2[1]; 
					$desc = mysql_escape_string(preg_replace('/(<a[^>]*>[^<]*<\/a>)|(Lamentamos[\s\S]+?producto.)/','',CFilter::SpHtml2Text($desc)));
					preg_match('/<span id="converterPrice" class="heroPrice">([^<]+)<\/span>/i', $content, $matches3);
					$price = $matches3[1];
					echo $name.'<br />';
					echo $desc.'<br />';
					echo $price.'<br />';
					$crawl->update("update zcshop_crawl_item set name='$name',description='$desc',price='$price' where id='$itemId'");

			}
		}
	}
	
	public function fixcrawldescAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		ignore_user_abort();
		set_time_limit(0);
		$crawl = new CCrawl();
		$data = $crawl->getAll();
		for($i=0;$i<count($data);$i++){
			$itemId = $data[$i]['id'];
			$desc=mysql_escape_string(preg_replace('/(<a[^>]*>[^<]*<\/a>)|(Lamentamos[\s\S]+?producto.)/','',CFilter::SpHtml2Text($data[$i]['description'])));
			$crawl->update("update zcshop_crawl_item set description='$desc' where id='$itemId'");
		}
	}
	
	public function updateslugAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$itemShow=new CItemShow();
		$itemShow->setType('product');
		$itemArray=$itemShow->getAllItems();	
		$product = new CProduct();
		foreach($itemArray as $item){
			$result = $product->editProducts(array($item['itemid']),array('slug'=>$item['subject']));
			echo $item['subject'].','.$item['itemid'].','.$result.'<br />';
		}
	}
	**/
	
	public function addmemberattrAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		$member = new CMember();
		$CFlashcommon = new CFlashcommon();
		$uidArr = $member->getAllUid();
		for($i=0;$i<count($uidArr);$i++){
			$CFlashcommon->addItem($uidArr[$i]['uid'],5);
		}

	}

}